package griffin.website;

//import griffin.website.contentlist.ItemClickListener;
import android.content.pm.ActivityInfo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import org.ksoap2.SoapEnvelope;
//import org.ksoap2.serialization.SoapObject;
//import org.ksoap2.serialization.SoapSerializationEnvelope;
//import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
//import android.app.AlertDialog;
//import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
//import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
//import android.widget.ImageButton;
import android.widget.ListView;
//import android.widget.RatingBar;
import android.widget.SimpleAdapter;
import android.widget.TextView;
//import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class SearchSite extends Activity {
	private static final String TAG = "[SearchSite]";
	private List<Map<String, Object>> resultList =  new ArrayList<Map<String, Object>>();
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.searchsite);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        Log.d(TAG, "[onCreate...] ");
        
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        
        Button btnSearch = (Button)this.findViewById(R.id.btnSearch);
        btnSearch.setOnClickListener(new SearchButtonListener());
    }
    
    class SearchButtonListener implements OnClickListener
    {
    	public void onClick(View v)
    	{
        	EditText etKeyword = (EditText)findViewById(R.id.etKeyword);
        	String keyword = etKeyword.getText().toString().trim();
        	
        	
    		getSearchResult(keyword);
    	}
    }
    
    private void getSearchResult(String keyword)
    {
    	if (!keyword.equals(""))
    	{
    		getSearchResultData(keyword);
    	}
		
		TextView tvRecordCount = (TextView)findViewById(R.id.tvRecordCount);
		tvRecordCount.setText(resultList.size() + " results");
		
    	ListView lvSearchResult = (ListView)findViewById(R.id.lvSearchResult);
		SimpleAdapter adapter = new SimpleAdapter(this,resultList,R.layout.searchresultitem,
			new String[]{"sitename"},
			new int[]{R.id.tvSiteName});
    	lvSearchResult.setAdapter(adapter);
    	lvSearchResult.setOnItemClickListener(new ItemClickListener());
    }
    
	private void getSearchResultData(String keyword) {
		resultList.clear();
		
		String databaseFilename = tabframework.DATABASE_PATH + "/" + tabframework.DATABASE_FILENAME;
		SQLiteDatabase db;

		db = SQLiteDatabase.openOrCreateDatabase(databaseFilename, null);
		Cursor result=db.rawQuery("SELECT VG_ID, VG_ENGLISH, VG_HTTPURL FROM VG_WebSites where VG_ENGLISH like '%" + keyword + "%'", null); 
		if (result.moveToFirst())
		{
			while (!result.isAfterLast()) {
				String siteid = result.getString(0);
				String sitename = result.getString(1);
				String siteurl = result.getString(2);
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("sitename", sitename);
				map.put("siteurl", siteurl);
				map.put("siteid", siteid);
				resultList.add(map);
				result.moveToNext(); 
			}
		}
		result.close(); 
		db.close();
	}
    
	class  ItemClickListener implements OnItemClickListener     
	{     
		public void onItemClick(AdapterView<?> arg0,//The AdapterView where the click happened      
				View arg1,//The view within the AdapterView that was clicked     
				int arg2,//The position of the view in the adapter     
				long arg3//The row id of the item that was clicked     
		) {      
			HashMap<String, Object> item=(HashMap<String, Object>) arg0.getItemAtPosition(arg2); 
			
			openUrl((String)item.get("siteurl"));
		}	
	}
	
	private void openUrl(String url) {
		Uri uri = Uri.parse(url);
		Intent intent = new Intent(Intent.ACTION_VIEW, uri);
		startActivity(intent); 
	}
	
}